//
//  MZNewestViewController.m
//  WHISPER
//
//  Created by lanou3g on 15/12/19.
//  Copyright © 2015年 王艳苹. All rights reserved.
//

#import "MZNewestViewController.h"
#import "MZNewestViewCell.h"
#import "YPMakeURL.h"
#import "YPNetWorking.h"
#import "MZNewestModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "YPWebViewVC.h"
@interface MZNewestViewController ()
@property (nonatomic, strong)NSMutableArray *newestArr;
@property (nonatomic, strong)NSString *total;
@end
static NSInteger num1 = 0;
@implementation MZNewestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MZNewestViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
    self.newestArr = [NSMutableArray array];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pullHeaderAction)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(pullFooterAction)];
    [self requestData];
    
    
}

- (void)requestData
{
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"addtime" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        self.total = (NSString *)dict[@"data"][@"total"];
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
    } fail:^(NSError *err) {
        
    }];

}

- (void)pullHeaderAction
{
    
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"addtime" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        [self.newestArr removeAllObjects];

        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError *err) {
        
    }];
    
}

- (void)pullFooterAction
{
    num1 += 10;
    [[YPNetWorking shareNetWorking] getDictWithURL:[[YPMakeURL shareMakeURL] readDetailURL] dict:[[YPMakeURL shareMakeURL] makeReadDetailDicWithStr:@"addtime" num1:num1 num2:self.type] success:^(NSDictionary *dict) {
        self.total = (NSString *)dict[@"data"][@"total"];
        for (NSDictionary *dic in dict[@"data"][@"list"]) {
            MZNewestModel *m = [[MZNewestModel alloc] init];
            [m setValuesForKeysWithDictionary:dic];
            [self.newestArr addObject:m];
        }
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError *err) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.newestArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MZNewestViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    MZNewestModel *m = self.newestArr[indexPath.row];
    cell.titleLabel.text = m.title;
    CGRect rect = [cell.titleLabel.text boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width - 16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : cell.titleLabel.font} context:nil];
    NSLog(@"%@",NSStringFromCGRect(rect));
    cell.titleLabel.frame = CGRectMake(cell.titleLabel.frame.origin.x, cell.titleLabel.frame.origin.y, rect.size.width, rect.size.height);
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.detailLabel.text = m.content;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 157;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MZNewestModel *model = self.newestArr[indexPath.row];
    YPWebViewVC *webVC = [[YPWebViewVC alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:webVC];
    NSString *str = [NSString stringWithFormat:@"http://pianke.me/webview/%@",model.contentID];
    webVC.tingID = str;
    webVC.type = @"读书";
    webVC.shareImage = model.coverimg;
    webVC.shareTitle = model.title;
    webVC.contendId = model.contentID;
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
